/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.gui.component;

import horizon.graphics.RenderingContext;
import horizon.util.Vector2;
import java.awt.Color;
import java.awt.event.MouseEvent;

/**
 *
 * @author J_Laptop
 */
public class GuiTextField extends GuiComponent {

    String text = "";
    Color textColor;
    Color bgColor;
    Color borderColor;
    int pointer = 0;

    public GuiTextField(String name, Color textColor, Color bgColor, Color borderColor, Vector2 size, Vector2 position) {
        super(name, size, position);
        this.textColor = textColor;
        this.bgColor = bgColor;
        this.borderColor = borderColor;


    }

    public Color getBgColor() {
        return bgColor;
    }

    public void setBgColor(Color bgColor) {
        this.bgColor = bgColor;
    }

    public Color getTextColor() {
        return textColor;
    }

    public void setTextColor(Color textColor) {
        this.textColor = textColor;
    }

    @Override
    public void update(long elapsedTime) {
    }

    @Override
    public void render(RenderingContext r) {

        r.drawFilledRectangle(componentBox, bgColor);
        if (inFocus) {
            r.drawRectangle(componentBox, Color.white);
        } else {
            r.drawRectangle(componentBox, borderColor);
        }

        int strWidth = r.getGraphics().getFontMetrics().stringWidth(text.substring(pointer));
        int strHeight = r.getGraphics().getFontMetrics().getHeight();

        if (strWidth > componentBox.getWidth() - 10) {
            pointer++;
        } else if (strWidth < componentBox.getWidth() - 10) {

            if (pointer > 0) {
                pointer--;
            }

        }



        r.drawText(text.substring(pointer), position.add(new Vector2(1, strHeight)), textColor);


    }

    public void setText(String s) {


        text = s;
        pointer = 0;

    }

    public String getText() {

        return text;

    }

    public void backspace() {

        if (text.length() > 0) {
            text = text.substring(0, text.length() - 1);
        }

    }

    @Override
    public void onActivate(MouseEvent e) {
       
    }

   
}
